package ca.ubc.icapture.genapha.forms;

import icapture.SQLMgr;
import icapture.beans.DB.Cohort;
import icapture.beans.DB.Phenotypes;
import icapture.beans.DB.Values;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Pattern;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.Resources;
import org.apache.struts.validator.ValidatorForm;

public class SearchAssociationForm extends ValidatorForm
{

    private ArrayList<Values> fullPvalueList;
    private ArrayList<Values> pvalueList;

    private ArrayList<Cohort> cohorts;
    private String[] selectedCohorts;
    private ArrayList<Phenotypes> phenotypes;
    private String[] selectedPhenotypes;

    private boolean chkPvalue;
    private ArrayList<String> availableCutType;
    private String cutType;
    private String cutValue;
    private boolean chkGene;
    private String genes;
    private boolean chkSnp;
    private String snps;
    private boolean chkChromosome;
    private String chromosomes;
    private boolean chkPathway;
    private String pathways;

    private String submitType;

    public SearchAssociationForm()
    {
        super();

        ArrayList<Cohort> cohortList = SQLMgr.getCohorts();
        Iterator<Cohort> cohortIter = cohortList.iterator();
        while (cohortIter.hasNext())
        {
            Cohort cohort = cohortIter.next();
            if (cohort.getName().contains("Hapmap"))
            {
                cohortIter.remove();
            }
        }
        setCohorts(cohortList);

        ArrayList<Phenotypes> phenotypeList = SQLMgr.getPhenotypes(1);
        Iterator<Phenotypes> phenoIter = phenotypeList.iterator();
        while (phenoIter.hasNext())
        {
            Phenotypes phenotype = phenoIter.next();
            if (phenotype.getName().equalsIgnoreCase("Asthma Ever"))
            {
                phenoIter.remove();
            }
        }
        setPhenotypes(phenotypeList);

        ArrayList<String> cutTypes = new ArrayList<String>();
        cutTypes.add("<=");
        cutTypes.add("<");
        cutTypes.add("=");
        cutTypes.add(">");
        cutTypes.add(">=");
        setAvailableCutType(cutTypes);
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request)
    {
        setChkPvalue(false);
        setChkSnp(false);
        setChkGene(false);
        setChkChromosome(false);
        setChkPathway(false);
    }

    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
    {
        ActionErrors errors = super.validate(mapping, request);

        // boolean to decide whether selectedCohorts and selectedPhenotypes are required
        boolean requiredPrimary = true;
        if (getSubmitType().equalsIgnoreCase("Filter")
            || (request.getParameter("noCache") != null && !request.getParameter("noCache").isEmpty()))
        {
            requiredPrimary = false;
        }

        // Validate selected cohorts
        if (requiredPrimary && getSelectedCohorts() == null)
        {
            errors.add("selectedCohorts", new ActionMessage("errors.required", Resources.getMessage(request, "assocSearch.cohort")));
        }

        // Validate selected phenotypes
        if (requiredPrimary && getSelectedPhenotypes() == null)
        {
            errors.add("selectedPhenotypes", new ActionMessage("errors.required", Resources.getMessage(request, "assocSearch.pheno")));
        }

        // Cut value must be a number
        if (isChkPvalue() && !Pattern.matches("^[0]?\\.\\d*$", getCutValue()))
        {
            errors.add("cutValue", new ActionMessage("errors.pvalue", Resources.getMessage(request, "assocSearch.pvalue")));
        }

        return errors;
    }

    /**
     * @return the fullPvalueList
     */
    public ArrayList<Values> getFullPvalueList()
    {
        return fullPvalueList;
    }

    /**
     * @param fullPvalueList the fullPvalueList to set
     */
    public void setFullPvalueList(ArrayList<Values> fullPvalueList)
    {
        this.fullPvalueList = fullPvalueList;
    }

    /**
     * @return the pvalueList
     */
    public ArrayList<Values> getPvalueList()
    {
        return pvalueList;
    }

    /**
     * @param pvalueList the pvalueList to set
     */
    public void setPvalueList(ArrayList<Values> pvalueList)
    {
        this.pvalueList = pvalueList;
    }

    /**
     * @return the cohorts
     */
    public ArrayList<Cohort> getCohorts()
    {
        return cohorts;
    }

    /**
     * @param cohorts the cohorts to set
     */
    public void setCohorts(ArrayList<Cohort> cohorts)
    {
        this.cohorts = cohorts;
    }

    /**
     * @return the selectedCohorts
     */
    public String[] getSelectedCohorts()
    {
        return selectedCohorts;
    }

    /**
     * @param selectedCohorts the selectedCohorts to set
     */
    public void setSelectedCohorts(String[] selectedCohorts)
    {
        this.selectedCohorts = selectedCohorts;
    }

    /**
     * @return the phenotypes
     */
    public ArrayList<Phenotypes> getPhenotypes()
    {
        return phenotypes;
    }

    /**
     * @param phenotypes the phenotypes to set
     */
    public void setPhenotypes(ArrayList<Phenotypes> phenotypes)
    {
        this.phenotypes = phenotypes;
    }

    /**
     * @return the selectedPhenotypes
     */
    public String[] getSelectedPhenotypes()
    {
        return selectedPhenotypes;
    }

    /**
     * @param selectedPhenotypes the selectedPhenotypes to set
     */
    public void setSelectedPhenotypes(String[] selectedPhenotypes)
    {
        this.selectedPhenotypes = selectedPhenotypes;
    }

    /**
     * @return the chkPvalue
     */
    public boolean isChkPvalue()
    {
        return chkPvalue;
    }

    /**
     * @param chkPvalue the chkPvalue to set
     */
    public void setChkPvalue(boolean chkPvalue)
    {
        this.chkPvalue = chkPvalue;
    }

    /**
     * @return the availableCutType
     */
    public ArrayList<String> getAvailableCutType()
    {
        return availableCutType;
    }

    /**
     * @param availableCutType the availableCutType to set
     */
    public void setAvailableCutType(ArrayList<String> availableCutType)
    {
        this.availableCutType = availableCutType;
    }

    /**
     * @return the cutType
     */
    public String getCutType()
    {
        return cutType;
    }

    /**
     * @param cutType the cutType to set
     */
    public void setCutType(String cutType)
    {
        this.cutType = cutType;
    }

    /**
     * @return the cutValue
     */
    public String getCutValue()
    {
        return cutValue;
    }

    /**
     * @param cutValue the cutValue to set
     */
    public void setCutValue(String cutValue)
    {
        this.cutValue = cutValue;
    }

    /**
     * @return the chkGene
     */
    public boolean isChkGene()
    {
        return chkGene;
    }

    /**
     * @param chkGene the chkGene to set
     */
    public void setChkGene(boolean chkGene)
    {
        this.chkGene = chkGene;
    }

    /**
     * @return the genes
     */
    public String getGenes()
    {
        return genes;
    }

    /**
     * @param genes the genes to set
     */
    public void setGenes(String genes)
    {
        this.genes = genes;
    }

    /**
     * @return the chkSnp
     */
    public boolean isChkSnp()
    {
        return chkSnp;
    }

    /**
     * @param chkSnp the chkSnp to set
     */
    public void setChkSnp(boolean chkSnp)
    {
        this.chkSnp = chkSnp;
    }

    /**
     * @return the snps
     */
    public String getSnps()
    {
        return snps;
    }

    /**
     * @param snps the snps to set
     */
    public void setSnps(String snps)
    {
        this.snps = snps;
    }

    /**
     * @return the chkChromosome
     */
    public boolean isChkChromosome()
    {
        return chkChromosome;
    }

    /**
     * @param chkChromosome the chkChromosome to set
     */
    public void setChkChromosome(boolean chkChromosome)
    {
        this.chkChromosome = chkChromosome;
    }

    /**
     * @return the chromosomes
     */
    public String getChromosomes()
    {
        return chromosomes;
    }

    /**
     * @param chromosomes the chromosomes to set
     */
    public void setChromosomes(String chromosomes)
    {
        this.chromosomes = chromosomes;
    }

    /**
     * @return the chkPathway
     */
    public boolean isChkPathway()
    {
        return chkPathway;
    }

    /**
     * @param chkPathway the chkPathway to set
     */
    public void setChkPathway(boolean chkPathway)
    {
        this.chkPathway = chkPathway;
    }

    /**
     * @return the pathways
     */
    public String getPathways()
    {
        return pathways;
    }

    /**
     * @param pathways the pathways to set
     */
    public void setPathways(String pathways)
    {
        this.pathways = pathways;
    }

    /**
     * @return the submitType
     */
    public String getSubmitType()
    {
        return submitType;
    }

    /**
     * @param submitType the submitType to set
     */
    public void setSubmitType(String submitType)
    {
        this.submitType = submitType;
    }
}